(function($){
	$.fn.productListInit = function(){
		var $this = $(this);
		var selectRow = null;
		var selectIndex = null;
		
		var editDialog= $('#editDialog',$this);
		//查询的下拉框
		$('#item',$this).combobox({
			data:TEA.getProductItemList(),
			valueField:'dataDictionaryID',
			textField:'dataDictionaryName',
			width:150
		})
		//查询
		$('#search',$this).click(function(){
			var queryContent = $('.queryContent',$this);
			var productItemID = $('#item',$this).combobox('getValue');
			var productCode = $('#code',queryContent).val();
			var productName = $('#name',queryContent).val();
			
			var content = {productCode:productCode,productName:productName,productItemID:productItemID};
			
			$('#productList',$this).datagrid({
				queryParams:content,
				pageNumber:1
			});
		})
		//修改
		var onUpdate = function(){
			if(selectRow==null){
				$.messager.alert("提示","请选择数据行","warning");
				return;
			}
			$(editDialog).dialog('open');
			$('#currentQuantity',editDialog).val(selectRow.currentQuantity);
		}
		//刷新列表
		var onReload = function(){
			$('#productList',$this).datagrid('reload');
		}
		//加载列表
		$('#productList',$this).datagrid({
			url:"product!select.action",
			singleSelect:true,
			fit:true,
			method:"POST",
			nowrap:true,
			striped: true,
			collapsible:true,
			rownumbers:true,
			pagination:true,
			pageSize:30,
			toolbar:[	
						{id:'update',text:'盘点',iconCls:'icon-edit',handler:function(){onUpdate()}},
						{id:'reload',text:'刷新',iconCls:'icon-reload',handler:function(){onReload()}}
					],
			columns:[[
				{field:'productID',hidden:true},
				{field:'productItemID',hidden:true},
				{field:'unitID',hidden:true},
				{field:'productItemName',title:'商品类别',width:100,align:"center"},
				{field:'productCode',title:'商品编号',width:100,align:"center"},
				{field:'productName',title:'商品名称',width:200,align:"center"},
				{field:'productSize',title:'规格',width:100,align:"center"},
				{field:'productPrice',title:'单价',width:100,align:"center"},
				{field:'unitName',title:'单位',width:100,align:"center"},
				{field:'currentQuantity',title:'当前库存数',width:100,align:"center"},
				{field:'note',title:'备注',width:300,align:"center"}
			]],
			onClickRow:function(rowIndex, rowData){
				selectRow = rowData;
				selectIndex = rowIndex;
			},
			onDblClickRow:function(rowIndex,rowData){
				onUpdate();
			}
		});
		
		var onSave = function(){
			var currentQuantity = $('#currentQuantity',editDialog).val();
			if($.trim(currentQuantity)==''){
				currentQuantity=0;
			}
			var url = 'product!inventory.action';
			var content={currentQuantity:currentQuantity,productID:selectRow.productID};
			asyncCallService(url,content,function(result){
				if(result.success){
					var fn = function(){
						selectRow.currentQuantity=currentQuantity;
						$('#productList',$this).datagrid('updateRow',{index: selectIndex,row:selectRow});
						onExit();
					}
					$.messager.alert("提示","盘点成功","info",fn);
				}else{
					$.messager.alert("提示","盘点出错","warning");
				}
			})
		}
		
		var onExit = function(){
			$(editDialog).dialog('close');
			$('#currentQuantity',editDialog).val('');
		}
		
		$(editDialog).dialog({  
		    title: '盘点商品',  
		    width:400,
		    height:300,
		    closed: true,  
		    cache: false,  
		    modal: true,
		    closable:false,
		    toolbar:[{text:'保存',iconCls:'icon-save',handler:function(){onSave()}},'-',
		    		 {text:'退出',iconCls:'icon-exit',handler:function(){onExit()}
			}]
		});
	}
})(jQuery);